package com.lx.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lx.dao.ResumeDao;
import com.lx.entity.Resume;

@WebServlet("/resume.do")
public class ResumeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String type = req.getParameter("type");
		
		ResumeDao resumedao = new ResumeDao();		
		if("add".equals(type)){
			Resume resume = requstgetGetData(req);
			HttpSession session = req.getSession();
			int applicant_id = (int) session.getAttribute("applicant_id");
			int basecinfoid = resumedao.add(resume, applicant_id);
			session.setAttribute("basecinfoid", basecinfoid);
			Resume resumes = new Resume();
			resumes = resumedao.queryBasec(basecinfoid);	
			req.setAttribute("resumes",resumes);
			req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);
		}
		if("query".equals(type)){
			HttpSession session = req.getSession();
			int basecinfoid = 0;
			if(session.getAttribute("basecinfoid")!=null){
				basecinfoid = (int) session.getAttribute("basecinfoid");
			}
			Resume resumes = new Resume();
			resumes = resumedao.queryBasecid(basecinfoid);	
			req.setAttribute("resumes",resumes);
			req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);
		}
		if("goupdate".equals(type)){
			HttpSession session = req.getSession();
			int basecinfoid = (int) session.getAttribute("basecinfoid");
			Resume resumes = new Resume();
			resumes = resumedao.queryBasecid(basecinfoid);
			req.setAttribute("resumes", resumes);
			req.getRequestDispatcher("applicant/resumeBasicinfoUpdate.jsp").forward(req, resp);
		}
		if("saveupdate".equals(type)){
			Resume resume = requstgetGetData(req);
			HttpSession session = req.getSession();
			int basecinfoid = (int) session.getAttribute("basecinfoid");
			Resume resumes = new Resume();
			resumedao.queryBasecUpdate(resume,basecinfoid);
			resumes = resumedao.queryBasecid(basecinfoid);	
			req.setAttribute("resumes",resumes);
			req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	private Resume requstgetGetData(HttpServletRequest req){
		
		 String realname = req.getParameter("realname");
		 String gender = req.getParameter("gender");
		 String birthday = req.getParameter("birthday");
		 String telephone  = req.getParameter("telephone");
		 String email = req.getParameter("email");
		 String job_intension = req.getParameter("job_intension");
		 String job_experience = req.getParameter("job_experience");
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		 Date birthdayD = null;
		 try{
			 birthdayD = sdf.parse(birthday);
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 Resume resume = new Resume(realname,gender,birthdayD,telephone,email,job_intension,job_experience);
		 return resume;
	}
	
}
